﻿//*********************************************************************
//
//  文件名：DebitService
//
//  作者： Pwp
//
//  作用：扣款处理类
//
//  日期：2015-10-14
//
//*********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YouTravel.DomainModels;
using YouTravel.Framework.Const;
using YouTravel.Framework.Enums;
using YouTravel.QueryModels;
using YouTravel.ViewModels;
using System.Data.Entity;
using Framework.Common.Exceptions;
using YouTravel.Framework.ResourceMessage;
using IRepositories.Core;
using EntityFramework.Data.Core;
using Framework.Common.Paged;
using Framework.Common;

namespace YouTravel.DomainService
{
	public class DebitService:IDebitService
	{
		readonly IEFRepository<Payment> _paymentRepository;

		readonly IUnitOfWork _db;
		public DebitService(IUnitOfWork db, IEFRepository<Payment> paymentRepository)
		{
			this._db = db;
			this._paymentRepository = paymentRepository;
			this._paymentRepository.SetDbContext(this._db);
		}

		/// <summary>
		/// 机构列表页面 Pwp 2015-09-16
		/// </summary>
		/// <param name="queryCond"></param>
		/// <returns></returns>
		public CustomPagedList<CollectInvoiceViewModel> GetList(PagedParam<CollectInvoiceQuery> queryCond)
		{
			return _paymentRepository.GetModel(o => o.Status == PaymentStatus.HasPayment)
				.Include(o => o.SellerSupplier)
				.Include(o => o.Special)
				.Include(o => o.CollectInvoice)
				.GetPagedList(queryCond.PageDto.PageNo,
				queryCond.PageDto.PageSize, BuildQueryCondition(queryCond.QueryDto),
				queryCond.SortField).ToCustomPagedList<Payment, CollectInvoiceViewModel>((u) =>
				{
					var s = u.MapNew<Payment, CollectInvoiceViewModel>();
					s.SupplyName = u.SellerSupplier.Name;
					if (u.CollectInvoice != null && u.CollectInvoice.Count > 0)
					{
						CollectInvoice c = u.CollectInvoice.OrderByDescending(o => o.CreateTime).FirstOrDefault();
						s.CollectInvoiceStatus = (int)c.Status;
						s.ConfirmTime = c.CreateTime;
					}
					else
					{
						s.CollectInvoiceStatus = (int)CollectInvoiceQueryStatus.NotSubmit;
					}
					return s;
				});
		}

		/// <summary>
		/// 查询条件处理
		/// </summary>
		/// <param name="branchQuery"></param>
		/// <returns></returns>
		private Func<IQueryable<Payment>, IQueryable<Payment>> BuildQueryCondition(CollectInvoiceQuery collectinvoiceQuery)
		{
			return (query) =>
			{
				if (collectinvoiceQuery != null)
				{
					if (collectinvoiceQuery.SpecialId != 0)
					{
						query = query.Where(p => p.SpecialId == collectinvoiceQuery.SpecialId);
					}
					if (collectinvoiceQuery.BranchId != 0)
					{
						query = query.Where(p => p.Special.BranchId == collectinvoiceQuery.BranchId);
					}
					if (!string.IsNullOrEmpty(collectinvoiceQuery.Name))
					{
						query = query.Where(p => p.Special.Name.Contains(collectinvoiceQuery.Name) || p.Special.ChargeName.Contains(collectinvoiceQuery.Name));
					}
					if (collectinvoiceQuery.Role == 2)
					{
						if (collectinvoiceQuery.Type != 0)
						{
							query = query.Where(p => p.CollectInvoice.OrderByDescending(o => o.CreateTime).FirstOrDefault().Status == (CollectInvoiceStatus)collectinvoiceQuery.Type);
						}
						else
						{
							query = query.Where(p => p.CollectInvoice.Count > 0);
						}
					}
					else
					//if(collectinvoiceQuery.Role == 1)
					{
						if (collectinvoiceQuery.Type != 0)
						{
							if (collectinvoiceQuery.Type == (int)CollectInvoiceQueryStatus.NotSubmit)
							{
								query = query.Where(p => p.CollectInvoice.Count < 1);
							}
							else
							{
								query = query.Where(p => p.CollectInvoice.OrderByDescending(o => o.CreateTime).FirstOrDefault().Status == (CollectInvoiceStatus)collectinvoiceQuery.Type);
							}
						}
					}
					if (collectinvoiceQuery.StartData.HasValue)
					{
						DateTime start = collectinvoiceQuery.StartData.Value.Date;
						query = query.Where(p => p.AuditTime >= start);
					}
					if (collectinvoiceQuery.EndData.HasValue)
					{
						DateTime end = collectinvoiceQuery.EndData.Value.AddDays(1).Date;
						query = query.Where(p => p.AuditTime < end);
					}
				}
				return query;
			};
		}

	}
}
